/**
 * Filename: YNBannerWebActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/18 1:03
 */
package com.github.postsanf.yinian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.TimeGoodsShopListAdapter;
import com.github.postsanf.yinian.bean.YNShop;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.entity.YNShopResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class TimeGoodsShopActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView timegood_shop_list;              // 列表视图
    private TimeGoodsShopListAdapter mAdapter;
    private List<YNShop> mShopList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_find_timegood_shop);
        initView();
        initShop();
    }

    private void initView() {
        findViewById(R.id.timegood_shopback).setOnClickListener(this);
        findViewById(R.id.timegood_shopcoupon).setOnClickListener(this);
        findViewById(R.id.timegood_shoporder).setOnClickListener(this);
        findViewById(R.id.title_shopcar).setOnClickListener(this);
        timegood_shop_list = (RecyclerView) findViewById(R.id.timegood_shop_list);
        timegood_shop_list.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new TimeGoodsShopListAdapter(TimeGoodsShopActivity.this, mShopList);
        timegood_shop_list.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.timegood_shopback:
                finish();
                break;
            case R.id.timegood_shopcoupon:
                intent2Activity(CouponListActivity.class);
                break;
            case R.id.timegood_shoporder:
                intent2Activity(OrderListActivity.class);
                break;
            case R.id.title_shopcar:
                intent2Activity(ShopCarListActivity.class);
                break;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initShop() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        StringRequest req = new StringRequest(getString(R.string.SHOP_URL) + getString(R.string.ynShowGoodsList))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNShopResponse resOfficialGroup = gson.fromJson(s, YNShopResponse.class);
                        if (resOfficialGroup != null && ReqUtils.isSuccess(resOfficialGroup.getCode())) {
                            mShopList.addAll(resOfficialGroup.getData());
                            mAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    private long fristTime;

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
        fristTime = System.currentTimeMillis();
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
        Intent intent = new Intent(ACTIONs.record);
        intent.putExtra("key", "goodsList_3");
        intent.putExtra("value", new Long(System.currentTimeMillis() - fristTime).intValue());
        sendBroadcast(intent);
    }
}
